import pickle
import numpy as np
import cv2

if __name__ == "__main__":
    # TODO: a simple example to load a saliency dataset file
    # RETURN: the values of the first record of the file. The record includes: timestamp, fixation_list, and saliency_map
    # note: this script assumes the dataset has been download from the LINK provided in the ./data folder
    """ class StrToBytes:
        def __init__(self, fileobj):
            self.fileobj = fileobj
    data = pickle.load(open('./data/saliency_ds1_topicparis','rb')) """
    def open_test_data():
        return open('./data/saliency_ds1_topicdiving', 'rb')
    print(open_test_data())

    def usr_data():
        with open_test_data() as f:
            return pickle.load(f, encoding='bytes')

    """ with open_test_data() as f:
        print(open_test_data())
        data = pickle.load(f)
        def read(self, size):
            return self.fileobj.read(size).encode()

        def readline(self, size=-1):
           return self.fileobj.readline(size).encode() """


    # load the dataset file named `saliency_ds1_topicparis` (ds=1, video=paris). Assuming the dataset file is in ./data folder
    """ f = open("./data/saliency_ds1_topicparis", 'rb')
    print(f.read())
    data = pickle.loads(f.read())
    f.close() """

    """ with open_test_data() as f:
            data = pickle.load(f) """
    data = usr_data()
    print("usr_data():", type(data), len(data))
    #access the first record
    timestamp, fixation_list, saliency_map = data[0]
    
    # print out the values of fields in the first record
    print('____________')
    print(timestamp)
    print(fixation_list)
    dimen = np.array(saliency_map).shape
    print('____________')
    print(dimen)
    print('____________')
    print(saliency_map[1].shape)
    cv2.imshow('image.jpg',saliency_map)
    cv2.waitKey(0)
